$(function(){

  // todo  配置 axios 请求根路径
  axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

$('#up2').on('click', () => {
    $('.two, .three').hide().siblings('.one').show()
    $('.one input').val('')
})


$('#up3').on('click', () => {
  $('.one, .three').hide().siblings('.two').show()
  $('.two input').val('')
}) 
 


  $('#btn1').on('click', async function () {
    // 获取密保问题
    const {data:res} = await axios({
      url: '/user/findPwd',
      params: {
        phone: $('#phone').val()
      },  
   })    
    // console.log(res);

    const {success, msg, data} = res
    if(success !== true) return alert(msg)
    $('.one, .three').hide().siblings('.two').show()
    console.log(data);    
  })


  $('#btn2').on('click',async function(){
      const {data:res} = await axios({
      url: '/user/findPwd',
      params: {
        phone: $('#phone').val()
      },        
     }) 
     console.log(res);

    //  对象解构
    const {success, msg, data} = res
    if(success !== true) return alert(msg)
     
    const username = document.querySelector('[name=username]') 
    const phone = document.querySelector('[name=phone]') 
    const question = document.querySelector('[name=question]') 
    const answer = document.querySelector('[name=answer]') 
    
    if(data.username === username.value&&data.phone === phone.value&&data.question === question.value){
        alert('验证通过');             
        $('.one, .two').hide().siblings('.three').show()
    }
    alert('验证未通过')
  })

  
  $('#btn3').on('click', async function(){
    const {data:res} = await axios({
      url: 'http://124.223.14.236:3001/api/user/findPwd',
      params: {
        phone: $('#phone').val()
      }             
   })
   
    console.log(res);
    const {success, msg, data} = res
    if(success !== true) return alert(msg)

    const {data:re} = await axios({
      method: 'POST',
      url: '/user/resetPwd',
      data: {
        id: String(data.id),
        password: $('[name=password]').val(),
        answer: $('[name=answer]').val(),
      }
    })
      console.log(re);
      if(re.success !== true) return alert('密保答案错误,请返回上一步')
      alert(re.msg)
      $('[name=password]').val('')
      //成功自动跳转至登录页   有问题不能跳转？
      location.href = './login.html'
 
  })



})